package connection.Data;

import java.sql.*;
import java.sql.DriverManager;

public class DatabaseConnection {
	public static final String driver = "org.postgresql.Driver";
	public static final String url = "jdbc:postgresql://127.0.0.1:5432";
	public static final String user = "postgres";
	public static final String password = "postgres";
	
	public Connection connection(){
		
		Connection conn = null;
		try {
			Class.forName(driver);
			try {
				conn = DriverManager.getConnection(url, user, password);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public void insertToStation(Connection conn, String[] rows) throws SQLException {
		String stmt = "insert into Station values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt = conn.prepareStatement(stmt);
		int numOfRows = rows.length;
		int i =1;
		//int j=1;
		while (i<=numOfRows)
		{
			pstmt.setString(i, rows[i-1]);
			//j++;
			i++;
		}
		pstmt.executeUpdate();
		System.out.println("Success..");

		
	}
	public void insertToWeather(Connection conn, String[] rows) throws SQLException {
		String stmt = "insert into Weather values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt = conn.prepareStatement(stmt);
		int numOfRows = rows.length;
		int i =1;
		//int j=1;
		while (i<=numOfRows)
		{
			pstmt.setString(i, rows[i-1]);
			//j++;
			i++;
		}
		pstmt.executeUpdate();
		System.out.println("Success..");

		
	}
}
